package com.leyou.page.mq;

import com.leyou.common.constants.MQConstants;
import com.leyou.page.service.PageService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.leyou.common.constants.MQConstants.Exchange.ITEM_EXCHANGE_NAME;
import static com.leyou.common.constants.MQConstants.Queue.*;

@Component
public class ItemListener {
    @Autowired
    private PageService PageService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = PAGE_ITEM_UP, durable = "true"),
            exchange = @Exchange(value = ITEM_EXCHANGE_NAME,type = ExchangeTypes.TOPIC),
            key = MQConstants.RoutingKey.ITEM_UP_KEY
    ))
    public void listenInsert(Long id) {
        if (id != null) {
            PageService.createItemHtml(id);
        }
    }


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = PAGE_ITEM_DOWN,durable = "true"),
            exchange = @Exchange(value = ITEM_EXCHANGE_NAME,type = ExchangeTypes.TOPIC),
            key = MQConstants.RoutingKey.ITEM_DOWN_KEY
    ))
    public void deleteItem(Long id) {
        if (id != null) {
            PageService.deleteItemHtml(id);
        }
    }
}
